package model;

import java.util.Objects;
import java.util.UUID;

/**
 * PackageName: model
 * ClassName: BaseModel
 * Author: tum0r
 * Time: 2020/5/23 15:37
 */
public class BaseModel {
    protected String UUID;
    protected String Fingerprint;
    protected double Size;
    protected String Unit = "B";

    private static final double B = 1.0;
    private static final int KB = 1024;
    private static final int MB = 1048576;
    private static final int GB = 1073741824;

    public String getUUID() {
        return UUID;
    }

    public void setUUID(String UUID) {
        this.UUID = Objects.requireNonNullElseGet(UUID, () -> java.util.UUID.randomUUID().toString().replace("-", ""));
    }

    public String getFingerprint() {
        return Fingerprint;
    }

    public void setFingerprint(String fingerprint) {
        Fingerprint = Objects.requireNonNullElseGet(fingerprint, String::new);
    }

    public double getSize() {
        return Size;
    }

    public void setSize(double size) {
        if (Math.abs(size - 0.0) >= 0) {
            Size = Double.parseDouble(String.format("%.2f", size));
        } else {
            Size = 0.0;
        }
    }

    public String getUnit() {
        return Unit;
    }

    public void setUnit(String unit) {
        Unit = unit;
    }

    protected BaseModel() {
        setUUID(null);
        setFingerprint("");
        setSize(0.0);
    }

    public void convertSize(long size) {
        double result = size / B;
        if (size >= GB) {
            result /= GB;
            Unit = "GB";
        } else if (size >= MB) {
            result /= MB;
            Unit = "MB";
        } else if (size >= KB) {
            result /= KB;
            Unit = "KB";
        }
        setSize(result);
    }
}
